package com.linkhub.oms.admin.modules.product.excel.listener;

import com.linkhub.oms.common.util.QtOptionalUtil;
import com.linkhub.oms.component.excel.listener.BaseDataListener;

import java.util.List;
import java.util.Set;

/**
 * @Description: BaseLocalProductExcelDataListener
 * @Author: echo
 * @Date: 2024/5/12 20:07
 * @Version: 1.0
 */
public abstract class BaseLocalProductExcelDataListener<T> extends BaseDataListener<T> {

    protected List<T> inValidDataList;
    protected Set<String> skuSets;
    protected Set<String> upcSets;
    protected Set<String> fnskuSets;
    protected Set<String> otherCodeSets;

    public BaseLocalProductExcelDataListener(List<T> list) {
        super(list);
    }

    public List<T> acquireInValidList() {
        return inValidDataList;
    }



    @Override
    protected boolean checkEmptyField(T data) {
        boolean emptyData = super.checkEmptyField(data);
        if (emptyData) {
            inValidDataList.add(data);
        }
        return emptyData;
    }

    @Override
    protected boolean predicate(T data) {
        if (repeat(getSku(data), skuSets)
                || repeat(getUpc(data), upcSets)
                || repeat(getFnsku(data), fnskuSets)
                || repeat(getOtherCode(data), otherCodeSets)) {
            inValidDataList.add(data);
            return false;

        }

        return true;
    }

    protected abstract String getSku(T data);

    protected abstract String getUpc(T data);

    protected abstract String getFnsku(T data);

    protected abstract String getOtherCode(T data);


    private boolean repeat(String value, Set<String> set) {
        return !QtOptionalUtil.of(value)
                .map(set::add)
                .orElse(true);
    }
}
